package com.ming.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.common.Result;
import com.ming.domain.SysPermission;
import com.ming.domain.SysUser;
import com.ming.from.SysRoleForm;
import com.ming.mapper.SysPermissionMapper;
import com.ming.mapper.SysUserMapper;
import com.ming.query.SysRoleQuery;
import com.ming.vo.SysPermissionVO;
import com.ming.vo.SysRoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.domain.SysRole;
import com.ming.mapper.SysRoleMapper;
import com.ming.service.SysRoleService;
import org.springframework.util.StringUtils;

/**
 *@Author:何元才
 *@Desc:
 *@data:2021-04-09 23:15
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    /**
     * 查询所有的角色
     *
     * @return
     */
    @Override
    public Result queryAllRoles() {
        // 查询列表数据
//        List<SysRoleVO> roles = sysRoleMapper.selectList(null);
        List<SysRoleVO> roles = sysRoleMapper.selectList((SysRoleQuery) null);
        return new Result(roles);
    }

    /**
     * 分页查询角色
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(SysRoleQuery query) {
        Page<SysRole> page = new Page<>(query.getPage(),query.getLimit());
        Page<SysRole> sysRolePage = sysRoleMapper.selectPage(page, new LambdaQueryWrapper<SysRole>()
                .like(StringUtils.hasText(query.getName()), SysRole::getName, query.getName())
        );
        return new Result(sysRolePage);
    }


    /**
     * 查询用户角色
     *
     * @param query
     * @return
     */
    @Override
    public Result queryUserRole(SysRoleQuery query) {
        List<SysRoleVO> roles = sysRoleMapper.selectListByUserId(query);
        return new Result(roles);
    }

    /**
     * 根据角色id 查询权限
     *
     * @param roleId
     * @return
     */
    @Override
    public Result rolePers(Integer roleId) {
        // 所有的权限
        List<SysPermissionVO> sysPermissionVOS = sysPermissionMapper.selectAll();
        //角色所有权限
        List<Integer> permissionIds = sysRoleMapper.selectPermissionIdByRole(roleId);
        for (SysPermissionVO sysPermissionVO : sysPermissionVOS) {
            Integer id = sysPermissionVO.getId();
            //判断角色是否有权限
            if (permissionIds.contains(id)){
                sysPermissionVO.setCheckArr("1");
            }
        }
        return new Result(sysPermissionVOS);
    }

    /**
     * 设置角色权限
     *
     * @param roleId
     * @param pId
     * @return
     */
    @Override
    public Result setPermissions(Integer roleId, List<Integer> pId) {
        //根据角色删除角色权限关系
        sysRoleMapper.deleteRelByRoleId(roleId);
        //批量新增角色权限关系
        if (!pId.isEmpty()){
            sysRoleMapper.batchInsertRolePerRel(roleId,pId);
        }
        return new Result();
    }

    /**
     * 修改角色信息
     *
     * @param form
     * @return
     */
    @Override
    public Result update(SysRoleForm form) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(form,sysRole);
        sysRoleMapper.updateById(sysRole);
        return new Result();
    }

    /**
     * 添加角色
     *
     * @param form
     * @return
     */
    @Override
    public Result add(SysRoleForm form) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(form,sysRole);
        sysRoleMapper.insert(sysRole);
        return new Result();
    }
}
